#!/bin/sh

echo "Content-Type: text/html"
echo

echo "<!-- $@ -->"
SAY() {
    echo "nmt:    $@"
}

VERSION=20090420-1

ROOT=$(echo $0 | sed 's/\/[^/]*$//')
ROOT=$(cd "$ROOT" ; pwd )


INSTALL() {
    #set -e
    #set -x
    read appname zipname install param 

    SAY "appname=$appname zipname=$zipname start=$install param=$param root=$ROOT"

    if echo "$appname" | egrep '^[a-zA-Z_0-9]+$' ; then
        TARGET="$ROOT/Apps/$appname"
        SAY "Target=$TARGET"
        mkdir -p "$TARGET"
        cd "$TARGET" &&  pwd && tar xf "$ROOT/$zipname.tar" && rm -f -- "$ROOT/$zipname.tar"
        chown -R nmt:nmt .
        #chmod 775 *.sh *.cgi *.bin *.php 2>/dev/null
        chmod 775 * 2>/dev/null
        SAY "ABOUT TO INSTALL : $TARGET/$install $param"
        "$TARGET/$install" "$param"
        SAY Done
    else
        SAY Cant determine application name
    fi
}

echo "$@" | sed 's/[&]/ /g' | INSTALL
exec rm "$0" #suicide ;)
